<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class social extends CI_Controller {
	public function __construct(){
		parent::__construct();
		parse_str($_SERVER['QUERY_STRING'], $_REQUEST);
		$this->load->library('facebook');		
	}
	function index(){
		
		
	}
	function facebook(){
		if($this->facebook->check_login()){
			$fb_user = $this->facebook->get_user();
			$fb_id = $fb_user['id'];
			$f = $this->facebook->get_friends();
			$user = $this->muser->get($fb_id,'facebook_id');
			if($user){
				$this->muser->setLogin($user);
			}
			else {
				$arr_insert = array();
				$arr_insert['facebook_id'] = $fb_id;
				$arr_insert['use_email'] = $fb_user['email'];
				$arr_insert['use_account'] = $this->muser->parse_email($arr_insert['use_email']);
				$arr_insert['use_name'] = $fb_user['name'];
				$arr_insert['use_gender'] = (($fb_user['gender'] == 'male')?1:0);
				$use_account = $arr_insert['use_account'];
				for($i = 1; $i< 1000; $i++){
					$check = $this->muser->get($arr_insert['use_account'],'use_account');
					if($check){
						$arr_insert['use_account'] = $use_account.'.'.$i;
					}
					else {
						break;
					}
				}
				$arr_insert['use_avatar'] =  $this->muser->download_avatar($this->facebook->get_avatar(400,400),$arr_insert['use_account']);
				$arr_insert['use_password'] =  $this->muser->random_key(10);
				$this->muser->registry($arr_insert);
				$this->muser->setLogin($arr_insert);
			}
			$referer = $this->session->userdata('game_referrer');
			if ($referer) {
				$this->session->unset_userdata('game_referrer');
				redirect($referer);
			}
			redirect(user_url('index'));
			
		}else {
			echo '<a href="'.$this->facebook->get_login_url().'">Login</a>';
		}
	}
	function check(){
		$f = $this->facebook->get_friends();
		print_r($f);
	}
	
}
?>